$(function(){
    var treeData = [];
    $.post($.webPath() + '/organ/gsjg/gsjg_tree?type=1',function(data){
        treeData = data;
    },'json');
    var nbgsTreeOptions = {
        url: $.webPath() + '/organ/gsjg/gsjg_tree?type=1',
        valueField:'id',
        textField:'text',
        panelHeight: '200',
        pageNumber: 1,
        required:true,
        loadFilter: function (rows) {
            return convert(rows);
        }
    };
    var nbgsKmdmys = {
        grid:$("#nbgsKmdmys_grid"),
        editingIndex:undefined,
        gridOptions:{
            url:$.webPath() + "/cwgl/kmys/nbgsKmdmys/nbgsKmdmysList",
//            updateUrl:$.webPath() +'/cwgl/kmys/nbgsKmdmys/save',
            toolbar:'#toolbar_nbgsKmdmys',
            fitColumns:true,
            fit:true,
            rownumbers:true,
            pageNumber: 1,
            pagination:true,
            singleSelect:true,
            columns:[
                [
                    {field:'nbgskmdmys.syId',title:'内部公司',width:70,editor:{type:'combotree',options:nbgsTreeOptions},formatter:function(value, row, index){
                        for (var i = 0; i < treeData.length; i++) {
//                            alert(value+treeData[i].text+treeData[i].id)
                            if (treeData[i].id == value) return treeData[i].text;
                        }
                        return value;
                    }},
                    {field:'nbgskmdmys.xyId',title:'内部映射公司',width:70,editor:{type:'combotree',options:nbgsTreeOptions},formatter:function(value, row, index){
                        for (var i = 0; i < treeData.length; i++) {
                            if (treeData[i].id == value) return treeData[i].text;
                        }
                        return value;
                    }},
                    {field:'nbgskmdmys.khkmdm',title:"客户辅助核算代码",editor:{type:'text',options:{required:true}},width:70},
                    {field:'nbgskmdmys.gyskmdm',title:"供应商辅助核算代码",editor:{type:'text',options:{required:true}},width:70},
                    {field:'cz',title:'操作',formatter:function(value,row,index){
                        if(value && value == 'save')
                            return '<a index="'+index+'" name="save" href="javascript:void(0)" onclick="save_nbgsKmdmys('+index+')"></a>'
                        return '<a index="'+index+'" name="edit" href="javascript:void(0)" onclick="edit_nbgsKmdmys('+index+')"></a>'
                    },width:21,align:'center'}
                ]
            ],
            onLoadSuccess:function(){
                $("a[name='edit']").linkbutton({
                    plain:true,
                    iconCls:'icon-edit'
                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onBeforeEdit:function(rowIndex, rowData){
                nbgsKmdmys.editingIndex = rowIndex;
                nbgsKmdmys.grid.edatagrid("updateRow",{
                    index: rowIndex,
                    row:$.extend({},rowData,{cz:'save'})

                });
                $("a[name='save']").linkbutton({
                    plain:true,
                    iconCls:'icon-save'
                });
            },
            onAfterEdit: function (index, row) {
//                alert(nbgsKmdmys.save(index,row));
                if(nbgsKmdmys.save(index,row)){
                    nbgsKmdmys.editingIndex = undefined;
                }else {
                    nbgsKmdmys.editingIndex = index;
                }
            }
        },
        save:function(index,row){
//            alert(nbgsKmdmys.grid.datagrid("validateRow",index));
            if(nbgsKmdmys.grid.datagrid("validateRow",index)){
                var url = $.webPath() +'/cwgl/kmys/nbgsKmdmys/save';
                var sucess = true;
                $.ajax({
                    url:url,
                    type:'post',
                    data:row,
                    async:false,
                    dataType:'json',
                    success:function(data){
                        if(data.statusCode == 200){
                            nbgsKmdmys.grid.datagrid('updateRow', {
                                index: index,
                                row:$.extend({},row,{cz:'edit'})
                            });
                            $("a[name='edit']").linkbutton({
                                plain:true,
                                iconCls:'icon-edit'
                            });
                        } else {
                            sucess = false;
                            $.messager.alert('info',data.message);
                            nbgsKmdmys.grid.edatagrid("beginEdit",index);
                        }
                    }
                });
                return sucess;
            }
            return false;
        },
        newItem:function(){
            if(nbgsKmdmys.editingIndex != undefined){
                nbgsKmdmys.grid.edatagrid("endEdit",nbgsKmdmys.editingIndex);
//                alert(nbgsKmdmys.editingIndex == undefined);
                if(nbgsKmdmys.editingIndex== undefined){
                    nbgsKmdmys.grid.edatagrid('insertRow',{
                        index: 0,
                        row: {}
                    });
                    nbgsKmdmys.grid.edatagrid("beginEdit",0);
                }
            } else {
                nbgsKmdmys.grid.edatagrid('insertRow',{
                    index: 0,
                    row: {}
                });
                nbgsKmdmys.grid.edatagrid("beginEdit",0);
            }
        },
        search:function(){
            nbgsKmdmys.grid.datagrid("load",{
                syId:$("#nbgsKmdmys_sy").combobox("getValue") || '',
                xyId:$("#nbgsKmdmys_xy").combobox("getValue") || '',
                khkmdm:$("#nbgsKmdmys_khkmdm").val() || '',
                gyskmdm:$("#nbgsKmdmys_gyskmdm").val() || ''
            });
        }
    }
    nbgsKmdmys.grid.edatagrid(nbgsKmdmys.gridOptions);
    $("#nbgsKmdmys_newItem").click(function(){
        nbgsKmdmys.newItem();
    });
    $("#nbgsKmdmys_sy").gsjgtree(1);
    $("#nbgsKmdmys_xy").gsjgtree(1);
    $("#btn_nbgsKmdmys_search").click(function(){
        nbgsKmdmys.search();
    });
});
function save_nbgsKmdmys(index){
    $("#nbgsKmdmys_grid").edatagrid("endEdit",index);
}
function edit_nbgsKmdmys(index){
    $("#nbgsKmdmys_grid").edatagrid("beginEdit",index);
}